<?php

namespace app\store\controller\shops;
 
use app\store\controller\BaseController;
use app\store\model\GoodsCategory;
use app\store\service\CategoryService;
use app\store\service\GoodsCategoryService;
use app\store\service\LabelService;
use think\exception\ValidateException;
use think\facade\View;

class LabelController extends BaseController
{
    /**
     * 分类列表
     */
    public function index()
    {   
        $keyword = $this->request->param('keyword','');

        $categoryService = new LabelService();
        $list = $categoryService->getCategoryList($keyword);


        return View::fetch('',['list' => $list]);
    }
    

    public function create()
    {
        // $goodsCategoryService = new GoodsCategoryService();
        // $options = $goodsCategoryService->getCategoryOption();

        View::engine()->layout(false);
        

        return View::fetch('');
    }

  
    /**
     * 编辑
     */
    public function edit()
    {   
        $id = $this->request->get('id/d');
        $categoryService = new LabelService();
        $row = $categoryService->findCategory($id);

        View::engine()->layout(false);

        return View::fetch('',['row' => $row]);
    }

    /**
     * 新增
     */
    public function save()
    {
        $param = $this->request->post();

        // try {
        //     validate('CategoryValidate')->check($param);
        // }catch(ValidateException $e) {
        //     return json(['code' => 0, 'msg' => $e->getError()]);
        // }

        $param['store_id'] = session('store_id');
        $id =  LabelService::saveGoodsCategory($param);

        return $id ? json(['code' => 1, 'msg' => '保存成功!']) : json(['code' => 0, 'msg' => '保存失败!']);
        
    }

    /**
     * 更新标签状态
     */
    public function switch()
    {
        $param = $this->request->post();
       
        $rst = LabelService::update($param);

        return $rst ? json(['code' => 1, 'msg' => '更新成功!']):json(['code' => 0, 'msg' => '更新失败!']);
    }

    
    /**
     * 更新
     */
    public function modify()
    {
        $param = $this->request->post();
    
        $rst =  LabelService::modify($param);

        return $rst ? json(['code' => 1, 'msg' => '更新成功!']) : json(['code' => 0, 'msg' => '更新失败!']);
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = $this->request->get('id/d');
      
        $res = LabelService::destroy($id);

        return  $res ? json(['code' => 1,'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败']);

    }
}